package com.alibaba.citrus.cr.forecast.common;

import com.alibaba.citrus.cr.common.log.ModuleEnum;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.log.model.OpLogModel;
import com.alibaba.cz.base.tool.log.model.OperatorType;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.user.dto.LoginUser;
import com.epoch.app.bcorder.platform_enhance.SaleOrderTag;
import com.epoch.app.crforecastsales.enums.ForecastStatusEnum;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class LogUtils {
    private static final Log log = Log.getLogger(LogUtils.class);

    public static void printOperateLog(ForecastOrderSDO forecastOrderSDO, Integer type, String activeValue, LoginUser loginUser) {
        //EmployeeGetCurrentLoginResponse employeeGetCurrentLoginResponse = bcOrderService.employeeGetCurrentLogin();
        //log.info("当前登陆人信息" + JSONObject.toJSONString(loginUser));
        OpLogModel.OpLogModelBuilder<String> builder = OpLogModel.builder();
        OpLogModel<String> opLogModel = builder.build();
        opLogModel.setModuleName(ModuleEnum.FORECAST_SALES.getModuleName());
        opLogModel.setModuleId(String.valueOf(ModuleEnum.FORECAST_SALES.getModuleId()));
        //ForecastOrderLogEnum 类型使用的枚举值，解析过程在纪元中
        opLogModel.setOperateType(type);
        opLogModel.setObjectId(forecastOrderSDO.getId());
        opLogModel.setObjectName("id");
        Map<String, String> map = new HashMap<>();
        map.put("name", loginUser.getRealName());
        map.put("active", activeValue);
        if (StringUtils.isNotBlank(forecastOrderSDO.getOrderBizId())){
            map.put("bizNo", forecastOrderSDO.getOrderBizId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getOrderType())) {
            SaleOrderTag saleOrderTag = SaleOrderTag.of(forecastOrderSDO.getOrderType());
            if (Objects.nonNull(saleOrderTag)) {
                map.put("orderType", saleOrderTag.getDesc());
            }
        }
        if (Objects.nonNull(forecastOrderSDO.getStatus())) {
            ForecastStatusEnum forecastStatusEnum = ForecastStatusEnum.of(forecastOrderSDO.getStatus());
            if (Objects.nonNull(forecastStatusEnum)) {
                map.put("status", forecastStatusEnum.getDesc());
            }
        }
        String logInfo = JSONObject.toJSONString(map);
        opLogModel.setFeatures(logInfo);
        opLogModel.setOperatorId(loginUser.getEmpId());
        opLogModel.setOperatorType(OperatorType.USER.getOperatorType());
        log.asyncMonitorObjectChangeV2(opLogModel);

    }
}
